// QuickLoadBar.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CharplanDoc.h"
#include "QuickLoadBar.h"
#include "dep/FolderDlg/FolderDlg.h"


// CQuickLoadBar-Dialogfeld

IMPLEMENT_DYNAMIC(CQuickLoadBar, CDialogBar)

CQuickLoadBar::CQuickLoadBar(CWnd* pParent /*= NULL*/) : CDialogBar()
{
    mb_show_message = true;
    mb_internal_update = false;
}

CQuickLoadBar::~CQuickLoadBar()
{
}

void CQuickLoadBar::DoDataExchange(CDataExchange* pDX)
{
    DDX_Control(pDX, IDC_QUICK_LOAD_COMBO, mc_combo);
    CDialogBar::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CQuickLoadBar, CDialogBar)
    ON_CBN_SELCHANGE(IDC_QUICK_LOAD_COMBO, &CQuickLoadBar::OnCbnSelchangeCombo1)
    ON_CBN_DROPDOWN(IDC_QUICK_LOAD_COMBO, &CQuickLoadBar::OnCbnDropdownCombo1)
    ON_MESSAGE(WM_INITDIALOG, OnInitDialog)
END_MESSAGE_MAP()



BOOL CQuickLoadBar::Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle,UINT nID)
{
    if (!CDialogBar::Create(pParentWnd,nIDTemplate,nStyle,nID)) return FALSE;

    CWnd* pc = GetDlgItem(IDC_BUTTON1);
    ASSERT(pc);
    if (pc)
    {
        CButton browsebutton;
        browsebutton.Attach(pc->GetSafeHwnd());

        browsebutton.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_FOLDER)));

        browsebutton.Detach();
    }

    mc_combo.SetColumnCount(5);
    mc_combo.SetColumnWidth(0, 150);
    mc_combo.SetColumnWidth(1, 50);
    mc_combo.SetColumnWidth(2, 50);
    mc_combo.SetColumnWidth(3, 50);
    mc_combo.SetDroppedWidth(300);

// FillFileList(GETOPTION_STR(_T("QUICKPATH")));

    return TRUE;
}

LONG CQuickLoadBar::OnInitDialog(UINT wParam, LONG lParam)
{
    LRESULT bRet = HandleInitDialog(wParam, lParam);

    if (!UpdateData(FALSE))
    {
        TRACE0("Warning: UpdateData failed during dialog init.\n");
    }

    m_realm_icons.Create(IDB_REALM_ICONS,15,0,RGB(255,255,255));
    mc_combo.SetImageList(&m_realm_icons);

    return bRet;
}


// CQuickLoadBar-Meldungshandler
void CQuickLoadBar::Reload()
{
    FillFileList(mstr_base_path);
}


void CQuickLoadBar::FillFileList(const CString& path)
{
    CWnd* pc = GetDlgItem(IDC_QUICK_LOAD_COMBO);
    ASSERT(pc);
    if (!pc) return;

    mc_combo.ResetContent();

    mstr_base_path = path;
    if (mstr_base_path.Right(1)!='\\' && mstr_base_path.Right(1)!='/') mstr_base_path += '\\';

    WIN32_FIND_DATA wfind;
    HANDLE fh = FindFirstFile(mstr_base_path + "*.chr",&wfind);
    if (fh != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (!(wfind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                CFileName fname(wfind.cFileName);

                int idx = mc_combo.AddString(fname.GetFileName());
                struct_head_info m_head;
                if (LoadCharHeadInfo(mstr_base_path + fname, m_head))
                {
                    CString temp;
                    mc_combo.SetItemText(idx,1,m_head.name_class);
                    temp.Format("%g",m_head.f_level);
                    mc_combo.SetItemText(idx,2,temp);
                    temp.Format("%iR%i",1+m_head.n_realmpoints/10,m_head.n_realmpoints%10);
                    mc_combo.SetItemText(idx,3,temp);

                    mc_combo.SetItemImage(idx,m_head.n_realm);
                    switch (m_head.n_realm)
                    {
                        case 0:
                            mc_combo.SetItemColor(idx,RGB(192,0,0));
                            break;
                        case 1:
                            mc_combo.SetItemColor(idx,RGB(0,128,0));
                            break;
                        case 2:
                            mc_combo.SetItemColor(idx,RGB(0,0,192));
                            break;
                    }

                }




            };
        }
        while (FindNextFile(fh,&wfind));
    }
    FindClose(fh);
}



void CQuickLoadBar::OnCbnSelchangeCombo1()
{
    CWnd* pc = GetDlgItem(IDC_QUICK_LOAD_COMBO);
    ASSERT(pc);
    if (!pc) return;

    int idx = mc_combo.GetCurSel();
    if (idx == CB_ERR) { return;}

    CString name;
    mc_combo.GetLBText(idx,name);

    mb_internal_update = true;
    ((CCharplanApp*)AfxGetApp())->QuickLoad(mstr_base_path + name + ".chr");
    mb_internal_update = false;

    mc_combo.SetFocus();
}


void CQuickLoadBar::Select(const CString& path)
{
    if (mb_internal_update) return;

    CWnd* pc = GetDlgItem(IDC_QUICK_LOAD_COMBO);
    ASSERT(pc);
    if (!pc) return;

    if (!IsWindow(mc_combo.m_hWnd)) return;
    for (int t=0;t<mc_combo.GetCount();t++)
    {
        CString temp;
        mc_combo.GetLBText(t,temp);
        if (path == mstr_base_path + temp + ".chr")
        {
            mc_combo.SetCurSel(t);
            return;
        }

    }

    mc_combo.SetCurSel(-1);
    mc_combo.SetWindowText(_T(""));
}


//////////////////////////////////////////////////////////////////////////
void CQuickLoadBar::Browse()
{
    CFolderDialog dlg(NULL, mstr_base_path, NULL, BIF_RETURNONLYFSDIRS);

    if (dlg.DoModal() == IDOK)
    {
        FillFileList(dlg.GetFolderPath());
        SETOPTION_STR(_T("QUICKPATH"),mstr_base_path);
    }
}

//////////////////////////////////////////////////////////////////////////
void CQuickLoadBar::OnCbnDropdownCombo1()
{
    if (mc_combo.GetCount()==0  && mb_show_message)
    {
        mb_show_message = false;
        AfxMessageBox(IDS_NOFILES);
    }
}
